package org.zcl.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zcl.entity.response.LotteryVO;
import org.zcl.service.AwardService;

/**
 * 彩票控制器
 *
 * @author zhuchenglin
 * @date 2022/09/07 13:47
 */
@Api(value = "redis", tags = {"彩票控制器"})
@RestController
@RequestMapping(value = "redis")
public class LotteryController {

    /**
     * 合同服务
     */
    @Autowired
    private AwardService awardService;

    /**
     * redisson客户
     */
    @Autowired
    private RedissonClient redissonClient;


    /**
     * init彩票池
     *
     * @param activityId 活动id
     * @return {@link Boolean}
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "activityId", value = "活动id")
    })
    @ApiOperation(value = "抽奖-初始化奖池", notes = "init彩票池", httpMethod = "POST")
    @PostMapping(value = "/initPool")
    public Boolean initLotteryPool(@RequestParam Integer activityId) {
        return awardService.initLotteryPool(activityId);
    }

    /**
     * 做彩票
     *
     * @param activityId 活动id
     * @return {@link LotteryVO}
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "activityId", value = "活动id")
    })
    @ApiOperation(value = "抽奖", notes = "doLottery", httpMethod = "GET")
    @GetMapping(value = "/doLottery")
    public LotteryVO doLottery(Integer activityId) {
        RLock lock = redissonClient.getLock("anyLock");
        lock.lock();
        try {
            return awardService.doLottery(activityId);
        } finally {
            lock.unlock();
        }
    }
}
